<?php
/*
	申请成为代理商
	如果已申请，但尚未审核通过，则页面显示请耐心等待管理员审核！
	如果已申请，且审核通过，则显示审核通过，并提供按钮前往代理中心
	如果审核不通过，则显示审核不通过原因
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	if(!$return['pluginset']['open_levediyfrom']){
		exi('未开启《等级申请》','error');
	}
	$member=$this->pdo_get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']));
	$apply=$this->get('leveapply',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']),array('id','remark'));
	if($apply['id']){
		$return['apply']=$apply;
		$return['applystatus']=$this->getcolumn('leveapply',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']),'status');
	}
	$return['member']=$member;
	$return['xieyi']=$this->plugin_setting('xieyi');
	$setting=$this->plugin_setting('page_setting');
	$levefrom_setting=$this->plugin_setting('levefrom_setting');
	$tagent=$this->getcolumn('level',array('id'=>$return['pluginset']['levefromid']),'title');
	$tagent=$tagent?:'上级';
	set_html('title','申请成为'.$tagent);
	$return['tagent']=$tagent;
	$return['setting']=$setting;
	$return['levefrom']=$levefrom_setting;
	$return['member']=$member?:false;
	$return['item']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));
	$return['item']['diyformfields']=$levefrom_setting['diyformfields']?:array();
	exi($return);
}
if($_RQ['op']=='apply'){
	$post=get_postdata();
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if(!$post['wechat']){
		exi('请输入微信号！','error');
	}
	if(!preg_match("/^1[345678]{1}\d{9}$/",$post['mobile'])){  	 
			exi('请输入正确的手机号码','error');
	}
	if($post['diyformfields']){
			foreach ($post['diyformfields'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
	}
	$this->model('member')->leveapply_agent($post);
	exi('申请成功！请耐心等待管理员审核！','','main');
}